<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Jadwal_model extends CI_Model {

	function __construct()
	{
		parent::__construct();
		$this->CI = get_instance();
	}
	
	//==================================================================
	function get_jadwalbelajar($id_sekolah, $id_ta){
		
		$this->db->select('*');
		$this->db->from('tb_jadwal_jam_belajar'); 
		$this->db->where('TAHUN_AJARAN',$id_ta);
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		// $this->db->where('ID_TINGKAT',$id_tingkat);
		// $this->db->where('ID_KELAS',$id_kelas);
		// $this->db->where('ID_SEMESTER',$id_smt);
		// $this->db->group_by('ID_JJB'); 
		$this->CI->flexigrid->build_query();
		
		$return['records'] = $this->db->get();
		
		$this->db->select('*');
		$this->db->from('tb_jadwal_jam_belajar'); 
		$this->db->where('TAHUN_AJARAN',$id_ta);
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		// $this->db->where('ID_TINGKAT',$id_tingkat);
		// $this->db->where('ID_KELAS',$id_kelas);
		// $this->db->where('ID_SEMESTER',$id_smt);
		$this->CI->flexigrid->build_query(FALSE);
		
		$return['record_count'] = $this->db->count_all_results();

		return $return;
	}
	
	function get_jadwalID($id_jjb){
		$this->db->select('*');
		$this->db->from('tb_jadwal_jam_belajar'); 
		$this->db->where('TAHUN_AJARAN',$this->session->userdata('tahunajaran'));
		$this->db->where('ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->db->where('ID_JJB',$id_jjb);
		// $this->db->where('ID_TINGKAT',$id_tingkat);
		// $this->db->where('ID_KELAS',$id_kelas);
		// $this->db->where('ID_SEMESTER',$id_smt);
		// if($jamke!='')
			// $this->db->where('JAM_KE',$id_smt);
		$query = $this->db->get();
		return $query;
	}
	
	function insert_jadwalbelajar($data){
		$this->db->insert('tb_jadwal_jam_belajar',$data);
	}
	
	function insert_jadwalpelajaran($data){
		$this->db->insert('tb_jadwal_pelajaran',$data);
	}
	
	function update_jadwalbelajar($data,$id_jjb){
		// $this->db->where('ID_SEKOLAH',$id_sekolah);
		// $this->db->where('TAHUN_AJARAN',$id_ta);
		$this->db->where('ID_JJB',$id_jjb);
		$this->db->update('tb_jadwal_jam_belajar',$data);
	}
	
	function delete_jadwalbelajar($id_jjb){
		$this->db->where('ID_JJB',$id_jjb);
		$this->db->delete('tb_jadwal_jam_belajar'); 
	}
	
	function delete_jadwal_pelajaran($id_jadwal){
		$this->db->where('ID_JADWAL_PELAJARAN',$id_jadwal);
		$this->db->delete('tb_jadwal_pelajaran'); 
	}
	
	function validasi_ph($id_sekolah,$id_ta,$id_tingkat, $id_kelas, $id_semester, $tglpresensi){
		
		//Select table column
		$this->db->select('count(*) as jml, ID_PRESENSI_HARIAN');
		$this->db->from('tb_presensi_hari');
		$this->db->where('TAHUN_AJARAN',$id_ta);
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		$this->db->where('ID_TINGKAT',$id_tingkat);
		$this->db->where('ID_KELAS',$id_kelas);
		$this->db->where('ID_SEMESTER',$id_semester);
		$this->db->where('TANGGAL',$tglpresensi);
		$query = $this->db->get();
		return $query;
	}
	
	function get_jadwalpelajaran($id_sekolah, $id_ta, $id_smt, $id_tingkat, $id_kelas ){
		
		$this->db->select(' a.* , NAMA_MATA_PELAJARAN, GELAR_DEPAN, NAMA_LENGKAP, GELAR_BELAKANG  ');
		$this->db->from('tb_jadwal_pelajaran a'); 
		$this->db->join('tb_pelajaran b','b.KODE_MATA_PELAJARAN=a.KODE_MATA_PELAJARAN', 'left'); 
		$this->db->join('tb_pegawai c','c.NIP=a.NIP', 'left'); 
		$this->db->where('a.ID_TAHUNAJARAN',$id_ta);
		$this->db->where('a.ID_SEKOLAH',$id_sekolah);
		$this->db->where('a.ID_TINGKAT',$id_tingkat);
		$this->db->where('a.ID_KELAS',$id_kelas);
		$this->db->where('a.ID_SEMESTER',$id_smt);
		$this->CI->flexigrid->build_query();
		
		$return['records'] = $this->db->get();
		
		$this->db->select(' a.* , NAMA_MATA_PELAJARAN, GELAR_DEPAN, NAMA_LENGKAP, GELAR_BELAKANG  ');
		$this->db->from('tb_jadwal_pelajaran a'); 
		$this->db->join('tb_pelajaran b','b.KODE_MATA_PELAJARAN=a.KODE_MATA_PELAJARAN', 'left'); 
		$this->db->join('tb_pegawai c','c.NIP=a.NIP', 'left'); 
		$this->db->where('a.ID_TAHUNAJARAN',$id_ta);
		$this->db->where('a.ID_SEKOLAH',$id_sekolah);
		$this->db->where('a.ID_TINGKAT',$id_tingkat);
		$this->db->where('a.ID_KELAS',$id_kelas);
		$this->db->where('a.ID_SEMESTER',$id_smt);
		$this->CI->flexigrid->build_query(FALSE);
		
		$return['record_count'] = $this->db->count_all_results();

		return $return;
	}
	
	function get_jumlahjadwal($id_sekolah, $id_ta){
		
		$this->db->select('*');
		$this->db->from('tb_jadwal_jam_belajar');
		$this->db->where('TAHUN_AJARAN',$id_ta);
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		
		$this->CI->flexigrid->build_query(FALSE);
		
		$return['record_count'] = $this->db->count_all_results();
		
		return $return;
	}
	
	function get_jumlahjadwal2($id_sekolah, $id_ta){
		
		$this->db->select('count(*) as JUMLAH');
		$this->db->from('tb_jadwal_jam_belajar');
		$this->db->where('TAHUN_AJARAN',$id_ta);
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		$query = $this->db->get()->row()->JUMLAH;
		return $query;
	}
	
	
	function cek_jjb($jamke){
		$this->db->select('count(*) as JUMLAH');
		$this->db->from('tb_jadwal_jam_belajar');
		$this->db->where('TAHUN_AJARAN',$this->session->userdata('tahunajaran'));
		$this->db->where('ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->db->where('JAM_KE',$jamke);
		$query = $this->db->get();
		return $query;
	}
	
	function cek_waktu_akhir($jamke){
		$this->db->select('HOUR(JAM_SELESAI) AS jamselesai, MINUTE(JAM_SELESAI) AS menitselesai');
		$this->db->from('tb_jadwal_jam_belajar');
		$this->db->where('TAHUN_AJARAN',$this->session->userdata('tahunajaran'));
		$this->db->where('ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->db->where('JAM_KE <',$jamke);
		$this->db->order_by('JAM_KE', 'desc');
		$this->db->limit(1);
		$query = $this->db->get();
		return $query;
	}
	
	function cek_waktu_mulai($jamke){
		$this->db->select('HOUR(JAM_MULAI) AS jammulai, MINUTE(JAM_MULAI) AS menitmulai');
		$this->db->from('tb_jadwal_jam_belajar');
		$this->db->where('TAHUN_AJARAN',$this->session->userdata('tahunajaran'));
		$this->db->where('ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->db->where('JAM_KE >',$jamke);
		$this->db->order_by('JAM_KE', 'asc');
		$this->db->limit(1);
		$query = $this->db->get();
		return $query;
	}
	
	function get_namakelas($idkelas){
		$this->db->select('KELAS');
		$this->db->from('tb_master_kelas'); 
		$this->db->where('ID_KELAS',$idkelas);
		$query = $this->db->get()->row()->KELAS;
		return $query;
	}

		
	function cek_jadwal_exist($id_tingkat, $id_kelas, $hari,$jamke){
		
		$this->db->select('count(*) as JUMLAH');
		$this->db->from('tb_jadwal_pelajaran');
		$this->db->where('ID_TAHUNAJARAN',$this->session->userdata('tahunajaran'));
		$this->db->where('ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->db->where('ID_SEMESTER',$this->session->userdata('semester'));
		$this->db->where('ID_KELAS',$id_kelas);
		$this->db->where('ID_TINGKAT',$id_tingkat);
		$this->db->where('HARI',$hari);
		$this->db->where('JAM_KE',$jamke);
		$query = $this->db->get()->row()->JUMLAH;
		return $query;
	}
	
	function cek_guruajar($jamke,$hari,$nip){
		
		$this->db->select('count(*) as JUMLAH');
		$this->db->from('tb_jadwal_pelajaran');
		$this->db->where('ID_TAHUNAJARAN',$this->session->userdata('tahunajaran'));
		$this->db->where('ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->db->where('ID_SEMESTER',$this->session->userdata('semester'));
		$this->db->where('HARI',$hari);
		$this->db->where('JAM_KE',$jamke);
		$this->db->where('NIP',$nip);
		$query = $this->db->get()->row()->JUMLAH;
		return $query;
	}
	
	function delete_jadwal_exist($id_tingkat, $id_kelas, $hari,$jamke){
		$this->db->where('ID_TAHUNAJARAN',$this->session->userdata('tahunajaran'));
		$this->db->where('ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->db->where('ID_SEMESTER',$this->session->userdata('semester'));
		$this->db->where('ID_KELAS',$id_kelas);
		$this->db->where('ID_TINGKAT',$id_tingkat);
		$this->db->where('HARI',$hari);
		$this->db->where('JAM_KE',$jamke);
		$this->db->delete('tb_jadwal_pelajaran'); 
	}
}
?>
